package com.netposa.datawarehouse.bodysearch.web.ext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.netposa.appwebres.exception.WebResException;
import com.netposa.npwonder.model.PvmPermission;
import com.netposa.npwonder.model.PvmUser;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class PvmContextExt {
   private static final Logger log = LoggerFactory.getLogger(PvmContextExt.class);
   private static final String TOKEN_ID_HEADER = "tokenId";
   private StringRedisTemplate stringRedisTemplate;
   private String WONDER_SYS_ID = "07";

   public PvmContextExt(StringRedisTemplate redisTemplate) {
      this.stringRedisTemplate = redisTemplate;
   }

   public PvmUser getPvmUser(HttpServletRequest request) {
      String tokenId = this.initInstanceParam(request);

      try {
         BoundHashOperations ops = this.stringRedisTemplate.boundHashOps(tokenId);
         Map infoVal = new HashMap();
         Iterator var5 = ops.keys().iterator();

         while(var5.hasNext()) {
            String key = (String)var5.next();
            infoVal.put(key, ops.get(key));
         }

         JSONObject pvmInfo = JSON.parseObject((String)infoVal.get("user"));
         if (pvmInfo == null) {
            log.error("Obtain user info is null.");
            throw new WebResException("501");
         } else {
            log.debug("Obtain user info is {}.", pvmInfo.toJSONString());
            PvmUser pvmUser = (PvmUser)JSON.toJavaObject(pvmInfo, PvmUser.class);
            boolean adminFlag = Boolean.parseBoolean((String)infoVal.get("isAdmin"));
            pvmUser.setAdmin(adminFlag);
            if (!adminFlag) {
               pvmUser.setFunPermission(JSON.parseObject((String)infoVal.get("function")).getJSONArray(this.WONDER_SYS_ID).toJavaList(PvmPermission.class));
               pvmUser.setRoles(JSON.parseArray((String)infoVal.get("role")).toJavaList(com.netposa.npwonder.model.PvmRole.class));
            }

            return pvmUser;
         }
      } catch (Exception var8) {
         log.error("Obtain user info exception.", var8);
         throw new WebResException("501");
      }
   }

   public List getPermission(HttpServletRequest request) {
      String tokenId = this.initInstanceParam(request);

      try {
         BoundHashOperations ops = this.stringRedisTemplate.boundHashOps(tokenId);
         String function = (String)ops.get("function");
         JSONArray funPer = JSON.parseObject(function).getJSONArray(this.WONDER_SYS_ID);
         log.debug("Obtain user permission info is {} .", funPer.toJSONString());
         return funPer.toJavaList(PvmPermission.class);
      } catch (Exception var6) {
         log.error("Obtain user permission exception .", var6);
         throw new WebResException("501");
      }
   }

   public List getRole(HttpServletRequest request) {
      String tokenId = this.initInstanceParam(request);

      try {
         BoundHashOperations ops = this.stringRedisTemplate.boundHashOps(tokenId);
         String role = (String)ops.get("role");
         JSONArray pvmInfo = JSON.parseArray(role);
         log.debug("Obtain user role info is {} .", pvmInfo.toJSONString());
         return pvmInfo.toJavaList(PvmRole.class);
      } catch (Exception var6) {
         log.error("Obtain user role exception.", var6);
         throw new WebResException("501");
      }
   }

   private String initInstanceParam(HttpServletRequest request) {
      String tokenId = "token#" + request.getHeader("tokenId");
      log.debug("Obtain request tokenId is {}.", tokenId);
      return tokenId;
   }
}
